using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.DataAccess;
using Thycotic.Collections;


namespace IGlobalist.Cms.Bll
{
    public class PageController : BaseController
    {
        private static IPage ConvertCmsObject2Page(ICmsObject obj)
        {
            if (obj == null) return null;
            IPage page = new Page();
            page.FromCmsObject(obj);
            return page;
        }


        private static IList<IPage> ConvertListObject2ListPages(IList<ICmsObject> list)
        {
            IList<IPage> pages = new List<IPage>();
            if (list != null)
            foreach (ICmsObject obj in list)
            {
                pages.Add(ConvertCmsObject2Page(obj));
            }
            //sort page

            PropertyComparer pc = new PropertyComparer();
            pc.AddPropertyName("Order", CompareOrder.Ascending);
            ArrayList listCmt = new ArrayList();
            foreach (Page p in pages)
                listCmt.Add(p);

            listCmt.Sort(pc);
            pages = new PageList();
            for (int i = 0; i < listCmt.Count; i++)
            {
                pages.Add((Page)listCmt[i]);
            }

            return pages;
        }


        public static PageList ConvertListObject2PageList(IList<ICmsObject> list)
        {

            PageList pageList = new PageList();
            if (list != null)
                foreach (ICmsObject obj in list)
                {
                    if (obj.ClassTypeID == (short)CmsObjectType.Page)
                    {
                        pageList.Add(ConvertCmsObject2Page(obj));
                    }
                }

            //sort page

            PropertyComparer pc = new PropertyComparer();
            pc.AddPropertyName("Order", CompareOrder.Ascending);
            ArrayList listCmt = new ArrayList();
            foreach (Page p in pageList)
                listCmt.Add(p);

            listCmt.Sort(pc);
            pageList = new PageList();
            for (int i = 0; i < listCmt.Count; i++)
            {
                pageList.Add((Page)listCmt[i]);
            }

            return pageList;
        }


        private static IPage ConvertObject2Pages(ICmsObject cmsObject)
        {
            IPage root = new Page();
            root.Name = "root";
            root.ID = 0;
            root.SubPageList = ConvertListObject2ListPages(cmsObject.SubObjectList,0);
            PopulatePage(root, Convert.ToInt64(cmsObject.ParentID));
            return root;
        }


        private static void PopulatePage(IPage parent, long parentID)
        {
            //if (parent.SubObjectList != null)
            //{
            //    foreach (IPage obj in parent.SubObjectList)
            //    {
            //        if (obj.Class.ClassTypeID== CmsObjectType.Page)
            //            PopulatePage(obj, parentID);
            //    }
            //}
        }


        private static PageList ConvertListObject2ListPages(IList<ICmsObject> list, long pageActiverID)
        {
            PageList pages = new PageList();

            //ArrayList arrList = list.GetSortedArrayList("Order", Thycotic.Collections.CompareOrder.Descending);

            if (list != null)
                foreach (ICmsObject obj in list)
                {
                    if (obj.ClassTypeID == (short)CmsObjectType.Page)
                    {
                        IPage page = ConvertCmsObject2Page(obj);
                        page.IsActive = (page.ID == pageActiverID);
                        pages.Add(page);
                    }
                }

            //sort page

            PropertyComparer pc = new PropertyComparer();
            pc.AddPropertyName("Order", CompareOrder.Ascending);
            ArrayList listCmt = new ArrayList();
            foreach (Page p in pages)
                listCmt.Add(p);

            listCmt.Sort(pc);
            pages = new PageList();
            for (int i = 0; i < listCmt.Count; i++)
            {
                pages.Add((Page)listCmt[i]);
            }

            return pages;
        }


        public static IPage GetPage(long id)
        {
            IPage page= ConvertCmsObject2Page(_Ds.GetObject(id));
            if (page!=null)
                page.CategoryList = CategoryController.GetCategoryListLelateToObjectID(id);
            return page;
        }

        public static IList<IPage> GetPageListForBindTree(long classID, long currentID)
        {
            IList<IPage> list = new List<IPage>();
            IPage root = PageController.GetPageTree();
            list.Add(root);
            
            PopulateBind(root, list,"",currentID);
            
            return list;
        }
		
		 private static void PopulateBind(IPage page, IList<IPage> list, string prefix,long currentID)
        {

            if (page.SubPageList != null)
            {
                prefix += "---";
                foreach (IPage p in page.SubPageList)
                {
                    if (p.ID != currentID)
                    {
                        p.Name = prefix + p.Name;
                        list.Add(p);
                        PopulateBind(p, list, prefix, currentID);
                    }
                }
                
            }
        }
		

        public static IPage GetPageTree()
        {
            IPage page = new Page();
            ICmsObject objRoot = _Ds.GetObjectListTree((short)CmsObjectType.Page,false);
            page = ConvertCmsObject2Page(objRoot);
            PopulateList(objRoot, page);
            return page;
        }


        private static void PopulateList(ICmsObject objRoot, IPage page)
        {
            bool add = true;
            page.SubPageList = new PageList();
            foreach (ICmsObject obj in objRoot.SubObjectList)
            {
                if (obj.ClassTypeID == (short)CmsObjectType.Page)
                {
                    //edit because page==forum
                    for (int i = 0; i < obj.ExtPropertyList.Count; i++)
                    {
                        if ((Convert.ToString(obj.ExtPropertyList[i].Name) == "TypeClass") && (Convert.ToString(obj.ExtPropertyList[i].Value) == "Forum"))
                        {
                            add = false;
                        }
                    }
                    if (add == true)
                    {
                        page.SubPageList.Add(ConvertCmsObject2Page(obj));
                        PopulateList(obj, page.SubPageList[page.SubPageList.Count - 1]);
                    }
                    else
                        add = true;
                }
            }
        }


        public static IPage GetPageCurrent(long id)
        {
            long ncontent=0;
            try
            {
               
                ICmsObject obj = _Ds.GetObject(id);
                if (obj == null)
                {
                    IList<ICmsObject> listPages = _Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Page);
                    obj = listPages[listPages.Count - 1];
                }

                IPage page = ConvertCmsObject2Page(obj);
                if (id > 0)
                {
                    IPage pCurrent = GetPage(id);
                    page.TemplateID = pCurrent.TemplateID;
                    page.ContentList = ContentController.ConvertListObject2ContentList(obj.SubObjectList);

                    //add for forum
                    ContentCategoryList catList = new ContentCategoryList();
                    ContentCategoryList catResult = new ContentCategoryList();
                    catList = CategoryController.ConvertListObject2ContentCategoryList(obj.SubObjectList);
                    foreach (Category c in catList)
                    {
                        ncontent = 0;
                        IContentCategory cat = c;
                        try
                        {
                            cat.ContentList = ContentController.GetContentListLelateToObjectID(c.ID, CmsObjectType.ContentCategory);
                            cat.CategoryList = CategoryController.GetCategoryListLelateToObjectID(c.ID);
                        }
                        catch
                        {
                        }
                        catResult.Add(cat);
                    }
                    page.CategoryList = catResult;
                }
                else
                 page.CategoryList = CategoryController.ConvertListObject2ContentCategoryList(obj.SubObjectList);

                if (page.CategoryList != null)
                {
                    for (int i = 0; i < page.CategoryList.Count; i++)
                    {
                        IContentCategory cat = page.CategoryList[i];
                        cat = CategoryController.GetCategoryNode(cat.ID);
                        if (cat != null)
                            page.CategoryList[i] = cat;
                    }
                }
                page.SubPageList = ConvertListObject2PageList(obj.SubObjectList);
                return page;
            }
            catch
            {
            }
            return null;
        }


        public static PageList GetPageListRelateToObjectID(long id)
        {


            ICmsObject obj = _Ds.GetObject(id);
            if (obj == null) return new PageList();
            IPage page = ConvertCmsObject2Page(obj);
            page.IsActive = true;
            page.IsCurrentPage = true;
            page.SubPageList = ConvertListObject2PageList(obj.SubObjectList);
            return page.SubPageList;
        }

        public static PageList GetObjectListRelateToObjectID(long id)
        {

          
            ICmsObject obj = _Ds.GetObject(id);
            if (obj == null) return new PageList();
            IPage page = ConvertCmsObject2Page(obj);
            page.IsActive = true;
            page.IsCurrentPage = true;
            page.SubPageList = ConvertListObject2PageList(obj.SubObjectList);

            if (page.ParentID == 0)
            {
                PageList pageList = ConvertListObject2ListPages(_Ds.GetTopObjectListByClassTypeID((short)CmsObjectType.Page), page.ID);
                for (int i = 0; i < pageList.Count; i++)
                {
                    if (pageList[i].ID == page.ID)
                    {
                        pageList[i] = page;

                    }

                }
                return pageList;
            }
            else
            {
                return PopulateListForRender(page);
            }
            
        }

       

        public static PageList PopulateListForRender(IPage page)
        {
            PageList pageList;
            IPage parent = GetPage(page.ParentID);
            if (parent == null)
            {
                pageList = ConvertListObject2ListPages(_Ds.GetTopObjectListByClassTypeID((short)CmsObjectType.Page),page.ID);

                for (int i = 0; i < pageList.Count; i++)
                {
                    if (pageList[i].ID == page.ID)
                    {
                        pageList[i] = page;

                    }

                }
                return pageList;
            }
            else
            {
                pageList = ConvertListObject2ListPages(_Ds.GetObjectListRelateToObjectID(page.ParentID, (short)CmsObjectType.Page), page.ID);
                for (int i = 0; i < pageList.Count; i++)
                {
                    if (pageList[i].ID == page.ID)
                    {
                        pageList[i] = page;

                    }

                }
                parent.SubPageList = pageList;
                parent.IsActive = true;
                return PopulateListForRender(parent);

            }

         
            
        }

        public static IList<IPage> GetTopObjectListByClassID(long classID)
        {
            return ConvertListObject2ListPages(_Ds.GetTopObjectListByClassID(classID));
        }
       
        public static IList<IPage> GetPageListByClass(long classID)
        {
            return ConvertListObject2ListPages(_Ds.GetObjectListByClassID(classID));
        }

        public static PageList GetPagesByClass(long classID)
        {
            PageList page = new PageList();
            IList<IPage> list = new List<IPage>();
            list= ConvertListObject2ListPages(_Ds.GetObjectListByClassID(classID));
            for (int i = 0; i < list.Count; i++)
            {
                IPage p = (IPage)list[i];
                IPage pCurrent = GetPageCurrent(p.ID);
                ICmsObject obj = _Ds.GetObject(p.ID);
                if (obj != null)
                {
                }
               // p.CategoryList = CategoryController.ConvertListObject2ContentCategoryList(obj.SubObjectList);

                page.Add(pCurrent);
                
            }
            return page;
          

        }

        public static IPage GetRootPages()
        {
            return ConvertObject2Pages(_Ds.GetObjectListTree((short)CmsObjectType.Page,false));
         
        }


        public static IList<IPage> GetPageListTreeByClass(long classID)
        {
            return ConvertListObject2ListPages(_Ds.GetObjectListByClassID(classID));
        }


        

        public static IPage GetPageTreeAll(long currentPageID)
        {
            
            IPage page = new Page();
            ICmsObject objRoot = _Ds.GetObjectListTree((short)CmsObjectType.Page,false);
            page = ConvertCmsObject2Page(objRoot);
            
            PopulateBindAll(objRoot, page);

            return page;
        }

        



        private static void PopulateBindAll(ICmsObject objRoot, IPage page)
        {
            if (page.SubPageList != null)
            {
                page.SubPageList = new PageList();
                page.CategoryList = new ContentCategoryList();
                page.ContentList = new ContentList();
             
                foreach (ICmsObject obj in objRoot.SubObjectList)
                {
                    if (obj.ClassTypeID == (short)CmsObjectType.ContentCategory)
                    {
                        IContentCategory cat = CategoryController.ConvertCmsObject2Category(obj);                      
                        page.CategoryList.Add(cat);
                    }

                    if (obj.ClassTypeID == (short)CmsObjectType.Content)
                    {
                        IContent content = ContentController.ConvertCmsObject2Content(obj);                      
                        page.ContentList.Add(content);
                    }

                    if (obj.ClassTypeID == (short)CmsObjectType.Page)
                    {
                        IPage p = ConvertCmsObject2Page(obj);                      
                        page.SubPageList.Add(p);
                        PopulateBindAll(obj, page.SubPageList[page.SubPageList.Count - 1]);
                    }
                }
            }
        }

        
   
        public static long InsertPage(IPage page)
        {            
            long id= _Ds.InsertObject((ICmsObject)page);
            
            InsertRelationCategoryList(page.CategoryList, id);
            return id;
        }

        private static void InsertRelationCategoryList(ContentCategoryList list, long pageID)
        {
            foreach (IContentCategory cat in list)
            {
                try
                {
                    _Ds.InsertObjectRelation(pageID, cat.ID, (short) CmsObjectType.Page);
                }
                catch { }
            }
        }


        public static void UpdatePage(IPage page)
        {
            ICmsObject obj = _Ds.GetObject(page.ID);
            ContentCategoryList categoryList = CategoryController.ConvertListObject2ContentCategoryList(obj.SubObjectList);
            
            _Ds.UpdateObject((ICmsObject)page);
           
            IList<long> listOld=new List<long>();
            foreach(IContentCategory cat in categoryList)            
            {
                listOld.Add(cat.ID);
            }
            IList<long> listNew = new List<long>();
            foreach (IContentCategory cat in page.CategoryList)
            {
                listNew.Add(cat.ID);
            }
            _Ds.UpdateObjectRelation(listOld, listNew, page.ID,(short) CmsObjectType.Page);
           
        }

        private static void DeleteRelationCategoryList(ContentCategoryList list, long pageID){
            foreach (IContentCategory cat in list)
            {
                try
                {
                    _Ds.DeleteObjectOneRelation(pageID, cat.ID);
                }
                catch { }
            }
        }

        public static void DeletePage(long id)
        {
            _Ds.DeleteObject(id);
        }


        #region page edu
            
        #endregion
    }
}
